/**
 * 单位unit.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    $("#unit_dataList").datagrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border: false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/unit/unitList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'unitName', title: '单位名称', width: 180,  fixed: true},
                {field: 'unitModel', title: '单位类别', width: 100,  fixed: true, formatter: function (val) {
                        if(val) {
                            return val.propertyName;
                        }
                    }},
                {field: 'unitProperty', title: '单位性质', width: 120,  fixed: true, formatter: function (val) {
                        if(val) {
                            return val.propertyName;
                        }
                    }},
                {field: 'unitOrgCode', title: '社会统一信用代码', width: 150,  fixed: true},
                {field: 'unitLeader', title: '单位法人', width: 80,  fixed: true},
                {field: 'unitMgr', title: '联系人', width: 80,  fixed: true},
                {field: 'unitMgrPhone', title: '联系人手机号', width: 150,  fit: true}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#unit_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#unit_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                lastChecked = null;
                $("#unit_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#unit_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/unit/toUnitOption.do";
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            unit_edit();
                        }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            unit_del();
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            unit_findList();
                        }
                    });
                }
                //刷新按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            unit_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除单位
 */
function unit_del() {
    var row = $("#unit_dataList").datagrid("getSelected");
    var checked = $("#unit_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除单位", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.unitName + ' 单位吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/unit/del.do?id=" + row.id,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#unit_dataList").datagrid("reload", {
                        unitName:$("#unitName_").val(),
                        unitOrgCode:$("#unitOrgCode_").val(),
                        unitModel:$("#unitModel_").combobox("getValue"),
                        unitProperty:$("#unitProperty_").combobox("getValue")
                    });
                }
            })
        }
    });
}

/**
 * 修改单位
 * @returns {boolean}
 */
function unit_edit(){
    var row = $("#unit_dataList").datagrid("getSelected");
    var checked = $("#unit_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title:'操作提示', msg:"请选择修改单位", timeout:3000, showType:"slide" });
        return;
    }
    window.location.href = CONTEXT_PATH + "/unit/toUnitOption.do?id=" + row.id;
}

/**
 * 单位详情
 */
function unit_view(iconCls){
    var row = $("#unit_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的单位详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(dwxq.id, dwxq.url + row.id, iconCls, dwxq.closable, dwxq.title);
}

//查询单位
function unit_findList(){
    refreshPageNumber("unit_dataList");
    $("#unit_dataList").datagrid("reload",{
        unitName:$("#unitName_").val(),
        unitOrgCode:$("#unitOrgCode_").val(),
        unitModel:$("#unitModel_").combobox("getValue"),
        unitProperty:$("#unitProperty_").combobox("getValue")
    });
    //清楚选中项
    $("#unit_dataList").datagrid("clearSelections");
}

/***
 * 动态刷新页面
 * @param prId
 */
function refreshPageNumber(csId) {
    //获取dataGrid的列表对象属性
    var $datagrid = $("#" + csId).datagrid("options");
    if ($datagrid != undefined) {
        $datagrid.pageNumber = 1;
    }
    //获取dataGrid的分页对象
    var $getPager = $("#" + csId).datagrid('getPager');
    var $pagination = $($getPager).pagination("options");
    if ($pagination != undefined) {
        $pagination.pageNumber = 1;
    }
}